<?php
/**
 * @file
 */

/**
 * Form builder; the client registration form.
 *
 * @ingroup forms
 * @see user_account_form()
 * @see user_account_form_validate()
 * @see user_register_submit()
 */
function nodejschat_client_form($form, &$form_state, $client = NULL) {
  global $user;
  $form['#attached']['library'][] = array('system', 'jquery.cookie');
  $form['#attributes']['class'][] = 'user-info-from-cookie';
  $form['#attributes']['class'][] = 'nodejschat-client-form';

  if (!$client) {
    $client = new StdClass();
    $client->modified = NODEJSCHAT_WAITING;
    $client->uid = $user->uid ? $user->uid : 0;
    $client->name = $user->name ? $user->name : '';
    $client->email = $user->mail ? $user->mail : '';
    $client->sid = session_id();
  }

  $form['#nodejschat_client'] = $client;

  // Start with the default user account fields.
  // Account information.
  $form['client'] = array(
    '#type'   => 'container',
    '#weight' => -10,
  );
  // Only show name field on registration form or user can change own username.
  $form['client']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#maxlength' => 255,
    '#required' => TRUE,
    '#attributes' => array('class' => array('nodejschat-client-name')),
    '#default_value' => !empty($client->name) ? $client->name : '',
    '#weight' => -10,
  );

  $form['client']['email'] = array(
    '#type' => 'textfield',
    '#title' => t('E-mail'),
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => !empty($client->email) ? $client->email : '',
    '#attributes' => array('class' => array('nodejschat-client-email')),
  );

  // Attach field widgets, and hide the ones where the 'user_register_form'
  // setting is not on.
  $langcode = entity_language('nodejschat_client', $form['#nodejschat_client']);
  field_attach_form('nodejschat_client', $form['#nodejschat_client'], $form, $form_state, $langcode);

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  $form['#validate'][] = 'nodejschat_client_validate';
  // Add the final user registration form submit handler.
  $form['#submit'][] = 'nodejschat_client_submit';

  drupal_add_library('system', 'jquery.form');

  return $form;
}

/**
 * Validation function for the client form.
 */
function nodejschat_client_validate($form, &$form_state) {
  entity_form_field_validate('nodejschat_client', $form, $form_state);
}

/**
 * Submit handler for the client form.
 */
function nodejschat_client_submit($form, &$form_state) {
  // Remove unneeded values.
  form_state_values_clean($form_state);

  $client = $form['#nodejschat_client'];

  entity_form_submit_build_entity('nodejschat_client', $client, $form, $form_state);

  // Populate $edit with the properties of $account, which have been edited on
  // this form by taking over all values, which appear in the form values too.
  $edit = array_intersect_key((array) $client, $form_state['values']);
  $client = nodejschat_client_save($client);

  // Terminate if an error occurred during user_save().
  if (!$client) {
    drupal_set_message(t("Error saving client."), 'error');
    $form_state['redirect'] = '';
    return;
  }
  $form_state['nodejschat_client'] = $client;
  $form_state['values']['ncid'] = $client->ncid;

  watchdog('nodejschat_client', 'New client: %name (%email).', array('%name' => $form_state['values']['name'], '%email' => $form_state['values']['mail']), WATCHDOG_NOTICE, l(t('edit'), 'nodejschat/client/' . $client->cid . '/edit'));
  $client = nodejschat_client_load_by_sid();
  if ($client->nodejschat) {
    $nodejschat = $client->nodejschat;
    drupal_goto('nodejschat/' . $nodejschat->cid);
  }
}
